import {
	appVersion
} from "@/api/common.js"
import {
	isEmpty
} from "@/util/helper.js"
const platform = uni.getSystemInfoSync().platform;

/**
 * ios新安装App时，未允许app访问网络前，插件[zhouWei-APPUpdate]无法请求，导致ios默认是审核模式
 * 此方法供登录成功跳转页面后调用
 */
export const getAppVersion = async () => {
	const synSetting = (status) => {
		getApp().globalData.is_audit_mode = status;
		// 广播给已经加载过的页面
		uni.$emit("appVersionCallback", status);
	}

	appVersion({
		os: platform == "android" ? "android" : "ios"
	}).then(res => {
		console.log("接口版本返回common---", res)
		const api_result = res.data;
		if (isEmpty(api_result) || isEmpty(api_result.downloadUrl)) {
			synSetting(false);
			return
		}

		// 是否shenhe模式
		if (api_result.is_audit_mode && api_result.is_audit_mode == 1 && api_result.versionCode ==
			httpData.version) {
			synSetting(true);
		} else {
			synSetting(false);
		}
	})
}

/**
 * 修改昵称、头像等信息后，同步更新本地存储的已登录账号中
 * @param {Object} opts
 * @param {String} opts.account_key 邮箱
 * @param {String} opts.fields_key 更新字段的key值
 * @param {String} opts.fields_value 更新字段的值
 */
export const handleUpdateLocalAccount = (opts) => {
	const {
		account_key,
		fields_key,
		fields_value
	} = opts;

	let _localAccountList = uni.getStorageSync("localAccountList");
	_localAccountList[account_key][fields_key] = fields_value;

	uni.setStorageSync("localAccountList", _localAccountList);
}